package com.sesame.controller;

import com.sesame.api.FMSConfig;
import com.sesame.service.NodeService;
import kim.sesame.framework.entity.GMap;
import kim.sesame.framework.utils.StringUtil;
import kim.sesame.framework.web.controller.AbstractWebController;
import kim.sesame.framework.web.response.Response;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@CommonsLog
@RestController
@RequestMapping("/node")
public class NodeController extends AbstractWebController {

    @Autowired
    private FMSConfig fmsConfig;

    @Autowired
    private NodeService nodeService;

    @RequestMapping("/tree")
    public Response tree() {

        List<GMap> list = nodeService.queryNodes(fmsConfig.getFilePath());

        return returnSuccess(list);
    }

    @RequestMapping("/getfilecontent")
    @ResponseBody
    public Response getfilecontent(String path) {

        List<String> list;
        try {
            path = StringUtil.removeQuotes(path);
            list = org.apache.commons.io.FileUtils.readLines(new File(path), "UTF-8");
        } catch (IOException e) {
            list = new ArrayList<String>();
            e.printStackTrace();
        }

        return returnSuccess(list);
    }
}
